package com.base.vistter.iframe.inverter.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.entity.InverterDevicesPowerDay;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.model.DevicesDayPowerRequest;
import com.base.vistter.iframe.service.InverterDevicesPowerDayService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/devices/power/day")
public class InverterDevicesPowerDayController {

    @Resource(name = "inverterDevicesPowerDayService")
    private InverterDevicesPowerDayService inverterDevicesPowerService;

    @PostMapping(value = "/statistics", produces = "application/json;charset=utf-8")
    public Result<List<InverterDevicesPowerDay>> statistics(@RequestBody DevicesDayPowerRequest devicesDayPowerReqest) throws IframeException {
        return Result.ok(inverterDevicesPowerService.statisticsDevices(devicesDayPowerReqest));
    }
    @GetMapping("/latest")
    public Result<InverterDevicesPowerDay> latest(Integer devicesId) throws IframeException {
        LambdaQueryWrapper<InverterDevicesPowerDay> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(InverterDevicesPowerDay::getInverterDevicesId, devicesId).orderByDesc(InverterDevicesPowerDay::getReportTime).last("LIMIT 1");
        return Result.ok(inverterDevicesPowerService.selectOne(lambdaQueryWrapper));
    }

}
